package com.vision.sales.advancepayment.cl.ui;

import static com.vision.sales.cl.SalesServices.advancePaymentSvc;

import java.sql.Timestamp;
import java.util.Date;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.CompanyComboBox;
import com.vision.sales.cl.event.AdvancePaymentCreatedEvent;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cl.ui.component.CustomerComboBox;
import com.vision.sales.cm.db.data.AdvancePayment;
import com.vision.sales.cm.db.data.AdvancePayment.FieldSpecs;

/**
 * 
 * @author Mark
 *
 */
public class CreateAdvancePaymentWindow implements IsWidget, Editor<AdvancePayment> {
	
	interface Binder extends UiBinder<Widget, CreateAdvancePaymentWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField Window window;
	@UiField VerticalLayoutContainer mainPanel;

	@UiField CompanyComboBox company;
	@UiField TextField advancePaymentNo;
	@UiField DateField date;
	@UiField NumberField<Double> amount;
	@UiField CustomerComboBox customer;
	@UiField TextArea remarks;
	
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;
	
	@UiField(provided = true) NumberFormat numberFormat = UI.amountFormat();
	@UiField(provided = true) NumberPropertyEditor<Double> doublePropertyEditor = UI.doubleEditor();
	
	interface AdvancePaymentDriver extends SimpleBeanEditorDriver<AdvancePayment, CreateAdvancePaymentWindow> { }
	
	private AdvancePaymentDriver driver = GWT.create(AdvancePaymentDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(SalesResources.INSTANCE.advancePayments());
		
		date.setPropertyEditor(UI.datePropertyEditor());
		date.setValue(new Date());
		
		driver.initialize(this);
		driver.edit(new AdvancePayment(new Timestamp(System.currentTimeMillis())));
		
		advancePaymentNo.addValidator(new MaxLengthValidator(FieldSpecs.AP_NO_MAX));
		remarks.addValidator(new MaxLengthValidator(FieldSpecs.REMARKS_MAX));
		
		loadData();
		
		return window;
	}
	
	// Listeners %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		AdvancePayment advancePayment = driver.flush();
		if (driver.hasErrors())
			return;
		
		UI.mask(window, "Creating advance payment...");
		advancePaymentSvc().create(advancePayment, new Callback<AdvancePayment>(window) {
			@Override
			protected void handleSuccess(AdvancePayment result) {
				window.hide(saveBtn);
				BaseModule.fireEvent(new AdvancePaymentCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	public void loadData() {
		Log.debug("Loading companies, customers, sales invoices, for dropdown...");

		company.loadCompanies();
		customer.loadCustomers();
	}
	
}
